package leetcode;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashSet;
import java.util.Set;

/**
 * @author 李栋
 * @version 1.0.0
 * @className Test3.java
 * @createTime 2021年08月10日 22:08:00
 */
public class Test3 {

    private static final Logger logger = LoggerFactory.getLogger(Test3.class);


    @Test
    public void test() {
        logger.info(String.valueOf(lengthOfLongestSubstring("aab")));
    }

    public int lengthOfLongestSubstring(String s) {
        char[] chars = s.toCharArray();
        if (chars.length == 2 && chars[0] != chars[1]) return 2;
        if (chars.length == 1) return 1;
        Set<String> set = new HashSet<>();
        for (int i = 0, charsLength = chars.length; i < charsLength; i++) {
            char aChar = chars[i];
            set.add(String.valueOf(aChar));
            for (int j = i + 1; j <= chars.length; j++) {
                String substring = s.substring(i, j);
                set.add(substring);
            }
        }
        String[] strings = set.toArray(new String[0]);
        int n = strings.length;
        for (int i = 0; i < n - 1; ++i) {
            int k = i;
            for (int j = i + 1; j < n; ++j) {
                if (strings[j].length() > strings[k].length()) {
                    k = j;
                }
            }
            if (k != i) {
                String temp = strings[i];
                strings[i] = strings[k];
                strings[k] = temp;
            }
        }

        for (String next : strings) {
            char[] chars1 = next.toCharArray();
            if (chars1.length == 1) return 1;
            if (checkOk(chars1)) {
                return next.length();
            }
        }
        return 0;
    }

    private boolean checkOk(char[] chars1) {
        Set<Character> set = new HashSet<>();
        for (char c : chars1) {
            set.add(c);
        }
        return chars1.length == set.size();
    }

    @Test
    public void testOne() {
        String s = "aab";
        int[] arr = new int[128];
        for (int i = 0; i < 128; i++) {
            arr[i] = -1;
        }
        int n = s.length();
        int start = 0;
        int res = 0;
        for (int i = 0; i < n; i++) {
            int index = s.charAt(i);
            start = Math.max(start, arr[index] + 1);
            res = Math.max(res, i - start + 1);
            arr[index] = i;
        }
    }

    @Test
    public void testTwo() {
        String s = "pwwkew";
        Set<Character> set = new HashSet<>();
        int n = s.length();
        int end = -1;
        int index = 0;
        for (int i = 0; i < n; i++) {
            if (0 != i) {
                set.remove(s.charAt(i - 1));
            }
            while (end + 1 < n) {
                boolean contains = set.contains(s.charAt(end + 1));
                if (contains) break;
                set.add(s.charAt(end + 1));
                end++;
            }
            index = Math.max(index, end - i + 1);
        }
        logger.info(String.valueOf(index));
    }


}
